/*
 * Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
 * See LICENSE in the project root for license information.
 */

// images references in the manifest
import "../../assets/icon-16.png";
import "../../assets/icon-32.png";
import "../../assets/icon-80.png";

// eslint-disable-next-line no-unused-vars
/* global console, document, Excel, Office */

let totalNameSet = new Set();

Office.onReady((info) => {
  if (info.host === Office.HostType.Excel) {
    // Determine if the user's version of Office supports all the Office.js APIs that are used in the tutorial.
    if (!Office.context.requirements.isSetSupported('ExcelApi', '1.7')) {
      console.log('Sorry. The tutorial add-in uses Excel.js APIs that are not available in your version of Office.');
    }

    // Assign event handlers and other initialization logic.
    document.getElementById("sideload-msg").style.display = "none";
    document.getElementById("app-body").style.display = "flex";
    document.getElementById("show-list").onclick = findUnsubmitMan;
    document.getElementById('datafile').addEventListener('change', readInputFile, false);

  }
});

/*
 * 读取本地文件
 */
function readInputFile(event) {
  let file = event.target.files[0];

  if (!file) {
    return;
  }

  let reader = new FileReader();

  reader.addEventListener('load', event => {
    let content = event.target.result.split("\r\n");
    totalNameSet.clear();
    for (let i = 0; i < content.length - 1; i++) {
      if (content[i] == "") continue;
      totalNameSet.add(content[i]);
    }
  });

  reader.readAsText(file);
}

/*
 * 显示未填写表格同学列表 
 */
function showNameList(res) {

  var nameListHtml = ""
  for (let item of res ) {
    nameListHtml += "<li>" + item + "</li> ";
  }
  document.getElementById("unsubmit-man").innerHTML = nameListHtml;
}

/*
 * 获取总行数
 */
function findUnsubmitMan() {
  Excel.run( async function (context) {
    var sheet = context.workbook.worksheets.getActiveWorksheet();
    var range = sheet.getUsedRange();
    range.load("address");
    await context.sync();
    let number = 0;
    let address = range.address;

    for (let i = address.length - 1; i >= 0; i--) {
      if (address[i] >= '0' && address[i] <= '9') {
        number = number * 10 + address[i] - '0';
      } else {
        break;
      }
    }

    var rangeAddress = "A2:A".concat(number);
    var range = sheet.getRange(rangeAddress);
    range.load('text');
    await context.sync();

    let s = new Set();
    let text = range.text;
    for (let item of text) { 
      if (item == "") continue;
      s.add(item[0]);
    }
   let res = difference(totalNameSet,s);
   showNameList(res);
  })
    .catch(function (error) {
      console.log("Error: " + error);
      if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
      }
    });
}


function difference(thisSet, otherSet) {
  let differenceSet = new Set();
  var values = Array.from(thisSet);
  for (var i = 0; i < values.length; i++) {
    if (!otherSet.has(values[i])) {
      console.log("val " + values[i])
      differenceSet.add(values[i])
    }
  }
  
  return differenceSet;
}




